package com.ttc.mBackup.BackupContacts;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import com.ttc.mBackup.R;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class RestoreContactActivity extends Activity {
	public class CustomAdapter extends ArrayAdapter<File> {
		private ArrayList<File> mItems;

		public CustomAdapter(Context context, int textViewResourceId,
				ArrayList<File> files) {
			super(context, textViewResourceId, files);
			mItems = files;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.c_files_sdcard_row, null);
			}
			File file = mItems.get(position);
			if (file != null) {
				TextView txtFileName = (TextView) v
						.findViewById(R.id.c_file_name);
				ImageView img = (ImageView) v.findViewById(R.id.c_file_icon);
				if (img != null) {
					img.setImageResource(R.drawable.json);
				}

				if (txtFileName != null) {
					txtFileName.setText(file.getName());
				}
			}

			return v;
		}
	}

	private ListView listView;
	ArrayList<File> files;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.c_files_sdcard);
		listView = (ListView) findViewById(R.id.listview_files);

		files = FileProvider.getBackupFiles();
		ListAdapter adapter = new CustomAdapter(this,
				R.layout.c_files_sdcard_row, files);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					final int arg2, long arg3) {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						RestoreContactActivity.this);
				builder.setMessage("Are you sure you want to restore?");
				builder.setCancelable(false);
				builder.setPositiveButton("Yes", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						File f = (File) listView.getAdapter().getItem(arg2);
						Log.i("filename", "" + f.getName());
						File sdcard = Environment.getExternalStorageDirectory();
						File file = new File(sdcard, f.getName());
						String text = "";
						try {
							BufferedReader br = new BufferedReader(
									new FileReader(file));
							String line = "";
							while ((line = br.readLine()) != null) {
								text += line;
							}
						} catch (IOException e) {
							// You'll need to add proper error handling here
						}
						JsonParser jp = new JsonParser();
						ArrayList<Contact> contacts = jp.fromJSONString(text);

						ContentResolver cr = getContentResolver();

						ContactDAO cd = new ContactDAO(cr);

						if (cd.writeContact(contacts) == true) {
							Toast.makeText(RestoreContactActivity.this,"Finished successfully",Toast.LENGTH_SHORT).show();
						} else {
							// Toast.makeText(this, "Finished successfully",
							// Toast.LENGTH_SHORT).show();
						}

					}
				});
				builder.setNegativeButton("No",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});

				AlertDialog alert = builder.create();
				alert.show();
			}
		});
	}

}
